package com.ssd.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ssd.entity.ChannelEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface ChannelDao extends BaseMapper<ChannelEntity> {

    @Select({
            "<script>",
            "SELECT",
            "c.id, c.channel_code, c.sku_id, c.vm_id, c.inner_code, c.max_capacity, c.current_capacity, c.last_supply_time,",
            "s.sku_name, s.sku_image, s.price as sku_price, s.class_id, s.is_discount, s.unit, s.brand_name",
            "FROM tb_channel c",
            "LEFT JOIN tb_sku s ON c.sku_id = s.sku_id",
            "<where>",
            "<if test='innerCode != null and innerCode != \"\"'>",
            "AND c.inner_code LIKE CONCAT('%', #{innerCode}, '%')",
            "</if>",
            "</where>",
            // 如果需要排序，可以在这里添加排序逻辑
            "</script>"
    })
    @Results(id = "channelMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "channelCode", column = "channel_code"),
            @Result(property = "skuId", column = "sku_id"),
            @Result(property = "vmId", column = "vm_id"),
            @Result(property = "innerCode", column = "inner_code"),
            @Result(property = "maxCapacity", column = "max_capacity"),
            @Result(property = "currentCapacity", column = "current_capacity"),
            @Result(property = "lastSupplyTime", column = "last_supply_time"),
            // 映射 SkuEntity 属性
            @Result(property = "sku.skuName", column = "sku_name"),
            @Result(property = "sku.skuImage", column = "sku_image"),
            @Result(property = "sku.price", column = "sku_price"),
            @Result(property = "sku.classId", column = "class_id"),
            @Result(property = "sku.discount", column = "is_discount"),
            @Result(property = "sku.unit", column = "unit"),
            @Result(property = "sku.brandName", column = "brand_name")
    })
    Page<Map<String, Object>> selectChannel(Page<?> page, String innerCode);

    /**
     * 批量插入渠道记录
     */
    @Insert("<script>" +
            "INSERT INTO tb_channel (channel_code, vm_id, inner_code, max_capacity, current_capacity, last_supply_time, create_time, update_time) " +
            "VALUES " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{item.channelCode}, #{item.vmId}, #{item.innerCode}, #{item.maxCapacity}, #{item.currentCapacity}, #{item.lastSupplyTime}, NOW(), NOW())" +
            "</foreach>" +
            "</script>")
    int batchInsertChannels(@Param("list") List<ChannelEntity> channelList);
}